import com.zjd.swing.browser.MyBrowserUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Description: ${description}
 * @Author: sobey
 * @Email: sobey178@163.com
 * @Date: 2022/3/27 15:41
 */
public class FormMain {
    private volatile static boolean openBaidu = false;
    private static Object openBaiDuLock = new Object();
    public static void main(String[] args)
    {
        JFrame frame=new JFrame("Java按钮组件示例");    //创建Frame窗口
        frame.setSize(400, 200);
        JPanel jp=new JPanel();    //创建JPanel对象
        JButton btn1=new JButton("我是普通按钮");    //创建JButton对象
        JButton btn2=new JButton("我是带背景颜色按钮");
        JButton btn3=new JButton("我是不可用按钮");
        JButton btn4=new JButton("我是底部对齐按钮");
        JButton btnBaidu = new JButton("百度");
        btnBaidu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                System.out.println("111");
                openBaidu = true;
            }
        });
        jp.add(btnBaidu);
        jp.add(btn1);
        btn2.setBackground(Color.YELLOW);    //设置按钮背景色
        jp.add(btn2);
        btn3.setEnabled(false);    //设置按钮不可用
        jp.add(btn3);
        Dimension preferredSize=new Dimension(160, 60);    //设置尺寸
        btn4.setPreferredSize(preferredSize);    //设置按钮大小
        btn4.setVerticalAlignment(SwingConstants.BOTTOM);    //设置按钮垂直对齐方式
        jp.add(btn4);
        frame.add(jp);
        frame.setBounds(300, 200, 600, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //TODO 添加定时器检查按钮点击时间
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                synchronized(openBaiDuLock){
                    if (openBaidu) {
                        openBaidu = false;
//                        MyBrowserUtils.open("百度","https://www.baidu.com");
                    }
                }
            }
        },1000,500);
    }
}
